package com.yehebl.orm.data.mysql;

import com.yehebl.orm.data.common.dto.ExpressResult;
import com.yehebl.orm.dto.ClassInfo;
import com.yehebl.orm.dto.FieldInfo;
import com.yehebl.orm.exception.OrmExpressionException;

import java.util.*;
import java.util.Map.Entry;


/**
 * 表达式解析
 */
public class MySqlExpressionManager {
    private static final Map<String, Expression> expressionMap = new HashMap<>();

    static {
        for (Expression expression : Expression.values()) {
            expressionMap.put(expression.name(), expression);
        }
    }

    enum Expression {
        /**
         * in 查询
         */
        IN,

        /**
         * not in查询
         */
        NIN,
        /**
         * 等于
         */
        EQ,
        /**
         * 不等于
         */
        NEQ,
        /**
         * 小于
         */
        LT,
        /**
         * 大于
         */
        GT,
        /**
         * 大于等于
         */
        GEQ,
        /**
         * 小于等于
         */
        LEQ,
        /**
         * 模糊查询
         */
        LIKE,
        /**
         * 右模糊查询
         */
        RLIKE,
        /**
         * 左模糊查询
         */
        LLIKE,
        /**
         * 排序
         */
        ORDER,

        /**
         * 页码
         */
        PAGE,

        /**
         * 长度
         */
        SIZE;

        public static Expression getEnum(String enumStr) {
            return expressionMap.get(enumStr);
        }
    }

    public static ExpressResult getUpdateSql(ClassInfo ci, Map<String, Object> params, Set<String> acceptSet) {
        return getUpdateSql(ci, params, MySqlSqlManager.getUpdate(ci, acceptSet));
    }

    public static ExpressResult getUpdateFilterSql(ClassInfo ci, Map<String, Object> params, Set<String> filterSet) {
        return getUpdateSql(ci, params, MySqlSqlManager.getUpdateFilter(ci, filterSet));
    }

    private static ExpressResult getUpdateSql(ClassInfo ci, Map<String, Object> params, String sql) {
        ExpressResult exResult = new ExpressResult();
        List<Object> list = new ArrayList<>();
        StringBuilder whereSql = new StringBuilder();
        StringBuilder orderSql = new StringBuilder();
        List<OrderIndex> orderList = new ArrayList<>();
        Integer page = null;
        Integer size = null;
        if (params != null) {
            page = (Integer) params.get(Expression.PAGE.toString());
            size = (Integer) params.get(Expression.SIZE.toString());
            for (Entry<String, Object> entry : params.entrySet()) {
                String expression = entry.getKey();
                int index = expression.indexOf("_");
                String fieldName = null;
                String expressionStr;
                FieldInfo fi = null;
                if (index != -1) {
                    expressionStr = expression.substring(0, index);
                    fieldName = expression.substring(index + 1);
                    fi = ci.getAllFieldMap().get(fieldName);
                } else {
                    expressionStr = expression;
                }
                Expression er = MySqlExpressionManager.Expression.getEnum(expressionStr);
                switch (er) {
                    case IN:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`in (");
                        Object object = entry.getValue();
                        if (object instanceof Object[]) {
                            Object[] pk = (Object[]) object;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else if (object instanceof Collection) {
                            Collection<?> pk = (Collection<?>) object;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else {
                            throw new OrmExpressionException("IN表达式数据类型错误");
                        }
                        whereSql.append(" )");
                        break;
                    case NIN:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`not in (");
                        Object object1 = entry.getValue();
                        if (object1 instanceof Object[]) {
                            Object[] pk = (Object[]) object1;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else if (object1 instanceof Collection) {
                            Collection<?> pk = (Collection<?>) object1;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else {
                            throw new OrmExpressionException("NIN表达式数据类型错误");
                        }
                        whereSql.append(" )");
                        break;
                    case EQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        if (entry.getValue() == null) {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("` is null");
                        } else {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("`=?");
                            list.add(entry.getValue());
                        }

                        break;
                    case NEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        if (entry.getValue() == null) {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("` is not null");
                        } else {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("`<> ?");
                            list.add(entry.getValue());
                        }
                        break;
                    case LIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : "%" + entry.getValue() + "%");
                        break;
                    case RLIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : entry.getValue() + "%");
                        break;
                    case LLIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : "%" + entry.getValue());
                        break;
                    case ORDER:
                        assert fieldName != null;
                        expressionOrder(fieldName, ci, entry.getValue(), orderList);
                        break;
                    case LT:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`< ?");
                        list.add(entry.getValue());
                        break;
                    case GT:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("` > ?");
                        list.add(entry.getValue());
                        break;
                    case LEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`<= ?");
                        list.add(entry.getValue());
                        break;
                    case GEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("` >= ?");
                        list.add(entry.getValue());
                        break;
                    case PAGE:
                    case SIZE:
                        break;
                    default:
                        throw new OrmExpressionException(expression);
                }
            }
        }
        String resultSql;
        getOrderStr(orderList, orderSql);
        if (whereSql.length() != 0) {
            whereSql.delete(0, 4);
            resultSql = sql + " where " + whereSql + orderSql;
        } else {
            resultSql = sql + orderSql;
        }
        if (page != null && size != null) {
            resultSql = resultSql + " limit " + ((page - 1) * size) + "," + size;
        } else if (size != null) {
            resultSql = resultSql + " limit " + size;
        }
        exResult.setSql(resultSql);
        exResult.setObjs(listToArray(list));
        return exResult;
    }

    public static ExpressResult getSelectSql(ClassInfo ci, String expression, Object obj) {
        ExpressResult exResult = new ExpressResult();
        String sql = MySqlSqlManager.getSelect(ci);
        int index = expression.indexOf("_");
        String fieldName = null;
        String expressionStr;
        FieldInfo fi = null;
        if (index != -1) {
            expressionStr = expression.substring(0, index);
            fieldName = expression.substring(index + 1);
            fi = ci.getAllFieldMap().get(fieldName);
        } else {
            expressionStr = expression;
        }
        Expression er = MySqlExpressionManager.Expression.getEnum(expressionStr);
        String resultSql;
        switch (er) {
            case IN:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                List<Object> list = new ArrayList<>();
                StringBuilder whereSql = new StringBuilder();
                whereSql.append(" `").append(fi.getTableFieldName()).append("`in (");
                if (obj instanceof Object[]) {
                    Object[] pk = (Object[]) obj;
                    int i = 0;
                    for (Object object2 : pk) {
                        if (i == 0) {
                            whereSql.append("?");
                        } else {
                            whereSql.append(",?");
                        }
                        i++;
                        list.add(object2);
                    }
                } else if (obj instanceof Collection) {
                    Collection<?> pk = (Collection<?>) obj;
                    int i = 0;
                    for (Object object2 : pk) {
                        if (i == 0) {
                            whereSql.append("?");
                        } else {
                            whereSql.append(",?");
                        }
                        i++;
                        list.add(object2);
                    }
                } else {
                    throw new OrmExpressionException("IN表达式数据类型错误");
                }
                whereSql.append(" )");
                resultSql = sql + " where " + whereSql;
                exResult.setObjs(listToArray(list));
                break;
            case NIN:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                List<Object> list1 = new ArrayList<>();
                StringBuilder whereSql1 = new StringBuilder();
                whereSql1.append(" `").append(fi.getTableFieldName()).append("`not in (");
                if (obj instanceof Object[]) {
                    Object[] pk = (Object[]) obj;
                    int i = 0;
                    for (Object object2 : pk) {
                        if (i == 0) {
                            whereSql1.append("?");
                        } else {
                            whereSql1.append(",?");
                        }
                        i++;
                        list1.add(object2);
                    }
                } else if (obj instanceof Collection) {
                    Collection<?> pk = (Collection<?>) obj;
                    int i = 0;
                    for (Object object2 : pk) {
                        if (i == 0) {
                            whereSql1.append("?");
                        } else {
                            whereSql1.append(",?");
                        }
                        i++;
                        list1.add(object2);
                    }
                } else {
                    throw new OrmExpressionException("NOT IN表达式数据类型错误");
                }
                whereSql1.append(" )");
                resultSql = sql + " where " + whereSql1;
                exResult.setObjs(listToArray(list1));
                break;
            case EQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                if (obj == null) {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "`is null";
                } else {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "`=?";
                }
                break;
            case NEQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                if (obj == null) {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "`is not null";
                } else {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "`<> ?";
                }
                break;
            case LIKE:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`like ?";
                obj = obj == null ? "%" : "%" + obj + "%";
                break;
            case RLIKE:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`like ?";
                obj = obj == null ? "%" : obj + "%";
                break;
            case LLIKE:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`like ?";
                obj = obj == null ? "%" : "%" + obj;
                break;
            case ORDER:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " order by `" + fi.getTableFieldName() + "` " + obj;
                obj = null;
                break;
            case LT:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`< ?";
                break;
            case GT:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`> ?";
                break;
            case LEQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`<= ?";
                break;
            case GEQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`>= ?";
                break;
            case SIZE:
                resultSql = sql + " limit ?";
                break;
            default:
                throw new OrmExpressionException(expression);
        }
        exResult.setSql(resultSql);
        exResult.setObj(obj);
        return exResult;
    }

    public static ExpressResult getSelectSql(ClassInfo ci, Map<String, Object> params) {
        ExpressResult exResult = new ExpressResult();
        List<Object> list = new ArrayList<>();
        String sql = MySqlSqlManager.getSelect(ci);
        StringBuilder whereSql = new StringBuilder();
        StringBuilder orderSql = new StringBuilder();
        List<OrderIndex> orderList = new ArrayList<>();
        Integer page = null;
        Integer size = null;
        if (params != null) {
            page = (Integer) params.get(Expression.PAGE.toString());
            size = (Integer) params.get(Expression.SIZE.toString());
            for (Entry<String, Object> entry : params.entrySet()) {
                String expression = entry.getKey();
                int index = expression.indexOf("_");
                String fieldName = null;
                String expressionStr;
                FieldInfo fi = null;
                if (index != -1) {
                    expressionStr = expression.substring(0, index);
                    fieldName = expression.substring(index + 1);
                    fi = ci.getAllFieldMap().get(fieldName);
                } else {
                    expressionStr = expression;
                }
                Expression er = MySqlExpressionManager.Expression.getEnum(expressionStr);
                switch (er) {
                    case IN:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`in (");
                        Object object = entry.getValue();
                        if (object instanceof Object[]) {
                            Object[] pk = (Object[]) object;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else if (object instanceof Collection) {
                            Collection<?> pk = (Collection<?>) object;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else {
                            throw new OrmExpressionException("IN表达式数据类型错误");
                        }
                        whereSql.append(" )");
                        break;
                    case NIN:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`not in (");
                        Object object1 = entry.getValue();
                        if (object1 instanceof Object[]) {
                            Object[] pk = (Object[]) object1;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else if (object1 instanceof Collection) {
                            Collection<?> pk = (Collection<?>) object1;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else {
                            throw new OrmExpressionException("NIN表达式数据类型错误");
                        }
                        whereSql.append(" )");
                        break;
                    case EQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        if (entry.getValue() == null) {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("` is null");
                        } else {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("`=?");
                            list.add(entry.getValue());
                        }

                        break;
                    case NEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        if (entry.getValue() == null) {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("` is not null");
                        } else {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("`<> ?");
                            list.add(entry.getValue());
                        }
                        break;
                    case LIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : "%" + entry.getValue() + "%");
                        break;
                    case RLIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : entry.getValue() + "%");
                        break;
                    case LLIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? null : "%" + entry.getValue());
                        break;
                    case ORDER:
                        assert fieldName != null;
                        expressionOrder(fieldName, ci, entry.getValue(), orderList);
                        break;
                    case LT:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`< ?");
                        list.add(entry.getValue());
                        break;
                    case GT:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("` > ?");
                        list.add(entry.getValue());
                        break;
                    case LEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`<= ?");
                        list.add(entry.getValue());
                        break;
                    case GEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("` >= ?");
                        list.add(entry.getValue());
                        break;
                    case PAGE:
                    case SIZE:
                        break;
                    default:
                        throw new OrmExpressionException(expression);
                }
            }
        }
        String resultSql;
        getOrderStr(orderList, orderSql);
        if (whereSql.length() != 0) {
            whereSql.delete(0, 4);
            resultSql = sql + " where " + whereSql + orderSql;
        } else {
            resultSql = sql + orderSql;
        }
        if (page != null && size != null) {
            resultSql = resultSql + " limit " + ((page - 1) * size) + "," + size;
        } else if (size != null) {
            resultSql = resultSql + " limit " + size;
        }
        exResult.setSql(resultSql);
        exResult.setObjs(listToArray(list));
        return exResult;
    }

    public static ExpressResult getSelectPageSql(ClassInfo ci, Map<String, Object> params) {
        StringBuilder sqlCount = new StringBuilder();
        StringBuilder sqlSb = new StringBuilder();
        ExpressResult exResult = new ExpressResult();
        List<Object> list = new ArrayList<>();
        String sql = MySqlSqlManager.getSelect(ci);
        StringBuilder whereSql = new StringBuilder();
        StringBuilder orderSql = new StringBuilder();
        List<OrderIndex> orderList = new ArrayList<>();
        if (params != null) {
            for (Entry<String, Object> entry : params.entrySet()) {
                String expression = entry.getKey();
                int index = expression.indexOf("_");
                if (index == -1) {
                    continue;
                }
                String expressionStr = expression.substring(0, index);
                String fieldName = expression.substring(index + 1);
                Expression er = MySqlExpressionManager.Expression.getEnum(expressionStr);
                FieldInfo fi = ci.getAllFieldMap().get(fieldName);
                switch (er) {
                    case IN:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`in (");
                        Object object = entry.getValue();
                        if (object instanceof Object[]) {
                            Object[] pk = (Object[]) object;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else if (object instanceof Collection) {
                            Collection<?> pk = (Collection<?>) object;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else {
                            throw new OrmExpressionException("IN表达式数据类型错误");
                        }
                        whereSql.append(" )");
                        break;
                    case NIN:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`not in (");
                        Object object1 = entry.getValue();
                        if (object1 instanceof Object[]) {
                            Object[] pk = (Object[]) object1;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else if (object1 instanceof Collection) {
                            Collection<?> pk = (Collection<?>) object1;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else {
                            throw new OrmExpressionException("NIN表达式数据类型错误");
                        }
                        whereSql.append(" )");
                        break;
                    case EQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        if (entry.getValue() == null) {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("` is null");
                        } else {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("`=?");
                            list.add(entry.getValue());
                        }
                        break;
                    case NEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        if (entry.getValue() == null) {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("` is not null");
                        } else {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("`<> ?");
                            list.add(entry.getValue());
                        }
                        break;
                    case LIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : "%" + entry.getValue() + "%");
                        break;
                    case RLIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : entry.getValue() + "%");
                        break;
                    case LLIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : "%" + entry.getValue());
                        break;
                    case ORDER:
                        expressionOrder(fieldName, ci, entry.getValue(), orderList);
                        break;
                    case LT:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`< ?");
                        list.add(entry.getValue());
                        break;
                    case GT:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("` > ?");
                        list.add(entry.getValue());
                        break;
                    case LEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`<= ?");
                        list.add(entry.getValue());
                        break;
                    case GEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("` >= ?");
                        list.add(entry.getValue());
                        break;
                    case PAGE:
                    case SIZE:
                        break;
                    default:
                        throw new OrmExpressionException(expression);
                }
            }
        }
        getOrderStr(orderList, orderSql);
        if (whereSql.length() != 0) {
            whereSql.delete(0, 4);
            sqlCount.append("select count(*) from `").append(ci.getTableName()).append("`").append(" where ").append(whereSql);
            sqlSb.append(sql).append(" where ").append(whereSql).append(orderSql);
        } else {
            sqlCount.append("select count(*) from `").append(ci.getTableName()).append("`");
            sqlSb.append(sql).append(orderSql);
        }
        exResult.setCountSql(sqlCount.toString());
        exResult.setSql(sqlSb.toString());
        exResult.setObjs(listToArray(list));
        return exResult;
    }

    public static ExpressResult getSelectCount(String expression, ClassInfo ci, Object obj) {
        ExpressResult exResult = new ExpressResult();
        String sql = "select count(*) from `" + ci.getTableName() + "`";
        int index = expression.indexOf("_");
        String fieldName = null;
        String expressionStr;
        FieldInfo fi = null;
        if (index != -1) {
            expressionStr = expression.substring(0, index);
            fieldName = expression.substring(index + 1);
            fi = ci.getAllFieldMap().get(fieldName);
        } else {
            expressionStr = expression;
        }
        Expression er = MySqlExpressionManager.Expression.getEnum(expressionStr);
        String resultSql;
        switch (er) {
            case IN:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                List<Object> list = new ArrayList<>();
                StringBuilder whereSql = new StringBuilder();
                whereSql.append(" `").append(fi.getTableFieldName()).append("`in (");
                if (obj instanceof Object[]) {
                    Object[] pk = (Object[]) obj;
                    int i = 0;
                    for (Object object2 : pk) {
                        if (i == 0) {
                            whereSql.append("?");
                        } else {
                            whereSql.append(",?");
                        }
                        i++;
                        list.add(object2);
                    }
                } else if (obj instanceof Collection) {
                    Collection<?> pk = (Collection<?>) obj;
                    int i = 0;
                    for (Object object2 : pk) {
                        if (i == 0) {
                            whereSql.append("?");
                        } else {
                            whereSql.append(",?");
                        }
                        i++;
                        list.add(object2);
                    }
                } else {
                    throw new OrmExpressionException("IN表达式数据类型错误");
                }
                whereSql.append(" )");
                resultSql = sql + " where " + whereSql;
                exResult.setObjs(listToArray(list));
                break;
            case NIN:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                List<Object> list1 = new ArrayList<>();
                StringBuilder whereSql1 = new StringBuilder();
                whereSql1.append(" `").append(fi.getTableFieldName()).append("`not in (");
                if (obj instanceof Object[]) {
                    Object[] pk = (Object[]) obj;
                    int i = 0;
                    for (Object object2 : pk) {
                        if (i == 0) {
                            whereSql1.append("?");
                        } else {
                            whereSql1.append(",?");
                        }
                        i++;
                        list1.add(object2);
                    }
                } else if (obj instanceof Collection) {
                    Collection<?> pk = (Collection<?>) obj;
                    int i = 0;
                    for (Object object2 : pk) {
                        if (i == 0) {
                            whereSql1.append("?");
                        } else {
                            whereSql1.append(",?");
                        }
                        i++;
                        list1.add(object2);
                    }
                } else {
                    throw new OrmExpressionException("NIN表达式数据类型错误");
                }
                whereSql1.append(" )");
                resultSql = sql + " where " + whereSql1;
                exResult.setObjs(listToArray(list1));
                break;
            case EQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                if (obj == null) {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "` is null";
                } else {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "`=?";
                }
                break;
            case NEQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                if (obj == null) {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "` is not null";
                } else {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "`<>?";
                }
                break;
            case LIKE:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`like ?";
                obj = obj == null ? "%" : "%" + obj + "%";
                break;
            case RLIKE:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`like ?";
                obj = obj == null ? "%" : obj + "%";
                break;
            case LLIKE:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`like ?";
                obj = obj == null ? "%" : "%" + obj;
                break;
            case LT:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`< ?";
                break;
            case GT:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "` > ?";
                break;
            case LEQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`<= ?";
                break;
            case GEQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "` >= ?";
                break;
            case SIZE:
                resultSql = sql + " limit ?";
                break;
            default:
                throw new OrmExpressionException(expression);
        }
        exResult.setSql(resultSql);
        exResult.setObj(obj);
        return exResult;
    }

    public static ExpressResult getDeleteSql(String expression, ClassInfo ci, Object obj) {
        ExpressResult exResult = new ExpressResult();
        String sql = MySqlSqlManager.getDelete(ci);
        int index = expression.indexOf("_");
        String fieldName = null;
        String expressionStr;
        FieldInfo fi = null;
        if (index != -1) {
            expressionStr = expression.substring(0, index);
            fieldName = expression.substring(index + 1);
            fi = ci.getAllFieldMap().get(fieldName);
        } else {
            expressionStr = expression;
        }
        Expression er = MySqlExpressionManager.Expression.getEnum(expressionStr);
        String resultSql;
        switch (er) {
            case IN:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                Object[] list;
                StringBuilder whereSql = new StringBuilder();
                whereSql.append(" `").append(fi.getTableFieldName()).append("`in (");
                if (obj instanceof Object[]) {
                    Object[] objects = (Object[]) obj;
                    list = objects;
                    forAppend(whereSql, objects);
                } else if (obj instanceof Collection) {
                    Collection<?> objects = (Collection<?>) obj;
                    list = listToArray(objects);
                    forAppend(whereSql, objects);
                } else {
                    throw new OrmExpressionException("IN表达式数据类型错误");
                }
                whereSql.append(" )");
                resultSql = sql + " where " + whereSql;
                exResult.setObjs(list);
                break;
            case NIN:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                Object[] list1;
                StringBuilder whereSql1 = new StringBuilder();
                whereSql1.append(" `").append(fi.getTableFieldName()).append("`not in (");
                if (obj instanceof Object[]) {
                    Object[] objects = (Object[]) obj;
                    list1 = objects;
                    forAppend(whereSql1, objects);
                } else if (obj instanceof Collection) {
                    Collection<?> objects = (Collection<?>) obj;
                    list1 = listToArray(objects);
                    forAppend(whereSql1, objects);
                } else {
                    throw new OrmExpressionException("NIN表达式数据类型错误");
                }
                whereSql1.append(" )");
                resultSql = sql + " where " + whereSql1;
                exResult.setObjs(list1);
                break;
            case EQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                if (obj == null) {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "` is null";
                } else {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "`=?";
                }
                break;
            case NEQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                if (obj == null) {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "` is not null";
                } else {
                    resultSql = sql + " where `" + fi.getTableFieldName() + "`=?";
                }
                break;
            case LIKE:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`like ?";
                obj = obj == null ? "%%" : "%" + obj + "%";
                break;
            case RLIKE:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`like ?";
                obj = obj == null ? "%" : obj + "%";
                break;
            case LLIKE:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`like ?";
                obj = obj == null ? "%" : "%" + obj;
                break;
            case LT:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`< ?";
                break;
            case GT:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`> ?";
                break;
            case LEQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`<= ?";
                break;
            case GEQ:
                if (fi == null) {
                    throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                }
                resultSql = sql + " where `" + fi.getTableFieldName() + "`>= ?";
                break;
            case SIZE:
                resultSql = sql + " limit ?";
                break;
            default:
                throw new OrmExpressionException(expression);
        }
        exResult.setSql(resultSql);
        exResult.setObj(obj);
        return exResult;
    }

    public static ExpressResult getDeleteSql(ClassInfo ci, Map<String, Object> params) {
        ExpressResult exResult = new ExpressResult();
        List<Object> list = new ArrayList<>();
        String sql = MySqlSqlManager.getDelete(ci);
        StringBuilder whereSql = new StringBuilder();
        StringBuilder orderSql = new StringBuilder();
        List<OrderIndex> orderList = new ArrayList<>();
        Integer page = null;
        Integer size = null;
        if (params != null) {
            page = (Integer) params.get(Expression.PAGE.toString());
            size = (Integer) params.get(Expression.SIZE.toString());
            for (Entry<String, Object> entry : params.entrySet()) {
                String expression = entry.getKey();
                int index = expression.indexOf("_");
                String fieldName = null;
                String expressionStr;
                FieldInfo fi = null;
                if (index != -1) {
                    expressionStr = expression.substring(0, index);
                    fieldName = expression.substring(index + 1);
                    fi = ci.getAllFieldMap().get(fieldName);
                } else {
                    expressionStr = expression;
                }
                Expression er = MySqlExpressionManager.Expression.getEnum(expressionStr);
                switch (er) {
                    case IN:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`in (");
                        Object object = entry.getValue();
                        if (object instanceof Object[]) {
                            Object[] pk = (Object[]) object;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else if (object instanceof Collection) {
                            Collection<?> pk = (Collection<?>) object;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else {
                            throw new OrmExpressionException("IN表达式数据类型错误");
                        }
                        whereSql.append(" )");
                        break;
                    case NIN:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`not in (");
                        Object object1 = entry.getValue();
                        if (object1 instanceof Object[]) {
                            Object[] pk = (Object[]) object1;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else if (object1 instanceof Collection) {
                            Collection<?> pk = (Collection<?>) object1;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else {
                            throw new OrmExpressionException("NIN表达式数据类型错误");
                        }
                        whereSql.append(" )");
                        break;
                    case EQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        if (entry.getValue() == null) {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("` is null");
                        } else {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("`=?");
                            list.add(entry.getValue());
                        }

                        break;
                    case NEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        if (entry.getValue() == null) {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("` is not null");
                        } else {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("`<> ?");
                            list.add(entry.getValue());
                        }
                        break;
                    case LIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%%" : "%" + entry.getValue() + "%");
                        break;
                    case RLIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : entry.getValue() + "%");
                        break;
                    case LLIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : "%" + entry.getValue());
                        break;
                    case ORDER:
                        assert fieldName != null;
                        expressionOrder(fieldName, ci, entry.getValue(), orderList);
                        break;
                    case LT:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`< ?");
                        list.add(entry.getValue());
                        break;
                    case GT:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("` > ?");
                        list.add(entry.getValue());
                        break;
                    case LEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`<= ?");
                        list.add(entry.getValue());
                        break;
                    case GEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("` >= ?");
                        list.add(entry.getValue());
                        break;
                    case PAGE:
                    case SIZE:
                        break;
                    default:
                        throw new OrmExpressionException(expression);
                }
            }
        }
        String resultSql;
        getOrderStr(orderList, orderSql);
        if (whereSql.length() != 0) {
            whereSql.delete(0, 4);
            resultSql = sql + " where " + whereSql + orderSql;
        } else {
            resultSql = sql + orderSql;
        }
        if (page != null && size != null) {
            resultSql = resultSql + " limit " + ((page - 1) * size) + "," + size;
        } else if (size != null) {
            resultSql = resultSql + " limit " + size;
        }
        exResult.setSql(resultSql);
        exResult.setObjs(listToArray(list));
        return exResult;
    }

    public static ExpressResult getSelectCount(ClassInfo ci, Map<String, Object> params) {
        ExpressResult exResult = new ExpressResult();
        List<Object> list = new ArrayList<>();
        String sql = "select count(*) from `" + ci.getTableName() + "`";
        StringBuilder whereSql = new StringBuilder();
        if (params != null) {
            for (Entry<String, Object> entry : params.entrySet()) {
                String expression = entry.getKey();
                int index = expression.indexOf("_");
                if (index == -1) {
                    continue;
                }
                String expressionStr = expression.substring(0, index);
                String fieldName = expression.substring(index + 1);
                Expression er = MySqlExpressionManager.Expression.getEnum(expressionStr);
                FieldInfo fi = ci.getAllFieldMap().get(fieldName);
                switch (er) {
                    case IN:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`in (");
                        Object object = entry.getValue();
                        if (object instanceof Object[]) {
                            Object[] pk = (Object[]) object;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else if (object instanceof Collection) {
                            Collection<?> pk = (Collection<?>) object;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else {
                            throw new OrmExpressionException("IN表达式数据类型错误");
                        }
                        whereSql.append(" )");
                        break;
                    case NIN:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`not in (");
                        Object object1 = entry.getValue();
                        if (object1 instanceof Object[]) {
                            Object[] pk = (Object[]) object1;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else if (object1 instanceof Collection) {
                            Collection<?> pk = (Collection<?>) object1;
                            int i = 0;
                            for (Object object2 : pk) {
                                if (i == 0) {
                                    whereSql.append("?");
                                } else {
                                    whereSql.append(",?");
                                }
                                i++;
                                list.add(object2);
                            }
                        } else {
                            throw new OrmExpressionException("NIN表达式数据类型错误");
                        }
                        whereSql.append(" )");
                        break;
                    case EQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        if (entry.getValue() == null) {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("` is null");
                        } else {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("`=?");
                            list.add(entry.getValue());
                        }
                        break;
                    case NEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        if (entry.getValue() == null) {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("` is not null");
                        } else {
                            whereSql.append(" and `").append(fi.getTableFieldName()).append("`<> ?");
                            list.add(entry.getValue());
                        }
                        break;
                    case LIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%%" : "%" + entry.getValue() + "%");
                        break;
                    case RLIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : entry.getValue() + "%");
                        break;
                    case LLIKE:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`like ?");
                        list.add(entry.getValue() == null ? "%" : "%" + entry.getValue());
                        break;
                    case LT:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`< ?");
                        list.add(entry.getValue());
                        break;
                    case GT:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`> ?");
                        list.add(entry.getValue());
                        break;
                    case LEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`<= ?");
                        list.add(entry.getValue());
                        break;
                    case GEQ:
                        if (fi == null) {
                            throw new OrmExpressionException(fieldName + ":字段不存在" + ci.getClazz().getName() + "中");
                        }
                        whereSql.append(" and `").append(fi.getTableFieldName()).append("`>= ?");
                        list.add(entry.getValue());
                        break;
                    case PAGE:
                    case SIZE:
                        break;
                    default:
                        throw new OrmExpressionException(expression);
                }
            }
        }
        if (whereSql.length() != 0) {
            whereSql.delete(0, 4);
            exResult.setSql(sql + " where " + whereSql);
        } else {
            exResult.setSql(sql);
        }
        exResult.setObjs(listToArray(list));
        return exResult;
    }

    private static void expressionOrder(String expression, ClassInfo ci, Object value, List<OrderIndex> list) {
        int index = expression.indexOf("_");
        if (index == -1) {
            FieldInfo fi = ci.getAllFieldMap().get(expression);
            list.add(new OrderIndex(list.size(), " `" + fi.getTableFieldName() + "` " + value + " "));
            return;
        }
        int orderIndex;
        try {
            orderIndex = Integer.parseInt(expression.substring(0, index));
        } catch (Exception e) {
            throw new OrmExpressionException(expression, e);
        }
        String fieldName = expression.substring(index + 1);
        FieldInfo fi = ci.getAllFieldMap().get(fieldName);
        list.add(new OrderIndex(orderIndex, " `" + fi.getTableFieldName() + "` " + value + " "));
    }

    private static void getOrderStr(List<OrderIndex> list, StringBuilder sb) {
        boolean flag = true;
        OrderIndex temp;
        int len = list.size();
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < len; j++) {
                if (list.get(j).getIndex() < list.get(i).getIndex()) {
                    temp = list.get(j);
                    list.set(j, list.get(i));
                    list.set(i, temp);
                }
            }
        }
        for (OrderIndex o : list) {
            if (flag) {
                sb.append(" order by ").append(o.getOrderSql()).append(" ");
                flag = false;
                continue;
            }
            sb.append(" , ").append(o.getOrderSql()).append(" ");
        }
    }

    private static Object[] listToArray(Collection<?> list) {
        Object[] objs = new Object[list.size()];
        int i = 0;
        for (Object object : list) {
            objs[i] = object;
            i++;
        }
        return objs;
    }

    /**
     * 拼接条件
     *
     */
    private static void forAppend(StringBuilder sb, Object[] objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i == 0) {
                sb.append("?");
                continue;
            }
            sb.append(",?");
        }
    }

    private static void forAppend(StringBuilder sb, Collection<?> objects) {
        int len = objects.size();
        for (int i = 0; i < len; i++) {
            if (i == 0) {
                sb.append("?");
                continue;
            }
            sb.append(",?");
        }
    }

}


class OrderIndex {
    public OrderIndex(int index, String orderSql) {
        this.index = index;
        this.orderSql = orderSql;
    }

    private int index;
    private String orderSql;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getOrderSql() {
        return orderSql;
    }

    public void setOrderSql(String orderSql) {
        this.orderSql = orderSql;
    }

}
